package com.stars.home.vmodel

import android.app.Application
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.config.ConfigRx
import com.stars.home.model.TechOrderDetail
import com.stars.home.utils.TimeUtils
import com.stars.home.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch
import java.util.ArrayList

class ReceivedDetailsVModel(application: Application) : BViewModel<BRepository>(application) {


    var id:String?=null
    var bean= MutableLiveData<TechOrderDetail>()

    var anonymous=MutableLiveData<Boolean>(false)//默认不匿名


    fun getMoneyType(payType:String?):String{

        when(payType){
            "1"->{
                return "余额支付"
            }
            "2"->{
                return "微信支付"
            }
            "3"->{
                return "支付宝支付"
            }
        }
        return ""
    }

    fun setTips(technicianStatus:Int?,serviceTime:String?):String{

        when(technicianStatus){
            -1->{
                return "已拒单 ($serviceTime)"
            }
            0->{
                return "待接单 ($serviceTime)"
            }
            5->{
                return "订单已完成 ($serviceTime)"
            }

        }
        return ""
    }

    fun toCopyText(orderNo: String?){

        val cm: ClipboardManager? = repository().context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager?
        val mClipData = ClipData.newPlainText("Label", orderNo)
        cm?.setPrimaryClip(mClipData)
        toast("订单号已复制到粘贴板")

    }


    fun toCancel(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.receiving(id,"-1") }
            if(it.ok(true)){
                //发送消息刷新--首页列表
                RxBus.get().post(ConfigRx.REFUSE_ORDER, "")

                getInfo()
            }
        }
    }

    fun toSure(){
        var isRecord= MMKV.defaultMMKV().getString("isRecord","4")
        if(isRecord.equals("0")){
            toast("您还为认证！，无法接单！")
            return
        }
        if(isRecord.equals("1")){
            toast("实名认证信息审核中暂时不能接单！请等待")
            return
        }
        if(isRecord.equals("-1")){
            toast("实名认证信息审核不通过！")
            return
        }
        if(isRecord.equals("3")){
            toast("请开启接单按钮！")
            return
        }

        if(TimeUtils.timeCompare(bean.value?.serviceTime, TimeUtils.getTime())==3){
            toast("已超过预约时间，无法接单！")
            return
        }
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.receiving(id,"1") }
            if(it.ok(true)){

                RxBus.get().post(ConfigRx.TAKE_ORDER, "")
                getInfo()
            }
        }
    }


    fun getInfo(){
        viewModelScope.launch{
            var it=repository().lreq { NetEngine.service.technicianOrderDetail(id) }
            if(it.ok()){
                bean.value=it.data
            }

        }
    }

    //----------------------已接单
    //图片
    var nopath: ArrayList<String> = ArrayList() //全路径
    var type=MutableLiveData<String>()


    //技师当前的位置
    var tenDistance:Double=0.0


    fun choseMoreImg(){
        //选择图片
        type.value="1"
    }
    fun clearMoreImg(position: Int) {
        nopath.removeAt(position)

    }

    fun setTipsTo(technicianStatus:Int?,serviceTime:String?):String{

        when(technicianStatus){

            1->{
                return "订单进行中($serviceTime)"
            }
            2->{
                return "订单进行中($serviceTime)"
            }
            3->{
                return "订单进行中($serviceTime)"
            }
            4->{
                return "订单进行中($serviceTime)"
            }

        }

        return ""
    }


    fun setButContent(technicianStatus:Int?):String{

        when(technicianStatus){
            1->{
                return "立即出发"
            }
            2->{
                return "到达目的地"
            }
            3->{
                return "开始服务"
            }
            4->{
                return "服务结束"
            }
        }

        return ""
    }

    fun toChangeStatus(technicianStatus:Int?){

        when(technicianStatus){
            1->{
                setChangeStatus("2")
            }
            2->{
                if(tenDistance>0&&tenDistance<100){
                    setChangeStatus("3")
                }else{
                   toast("您距离客人还有一定距离哦！")
                }

            }
            3->{
                setChangeStatus("4")
            }
            4->{
                setChangeStatus("5")
            }
        }

    }

    fun setChangeStatus(type:String){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.updateTechnicianStatus(id,type) }
            if(it.ok(true)){

                getInfo()
                if(type=="5"){
                    RxBus.get().post(ConfigRx.REFUSE_ORDER, "")
                }
            }
        }
    }

    fun toUpImg(){
        if(nopath.size==0||nopath.size<3){

            toast("请上传三张图片")
            return
        }
        var img=""
        for(item in nopath){
            img = if (img == ""){
                item
            }else{
                "$img,$item"
            }
        }
        viewModelScope.launch {
            val resp = repository().lreq { NetEngine.service.achieveImage(id,img) }
            if (resp.ok(true)) {
                //刷新
                getInfo()
            }
        }


    }



}



